﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using System.Linq.Dynamic;
using LINQtoSQLDataProvider;


namespace Dragon.Prototype1.Service
{
    public class WarehouseService
    {
        static WMSDataContext shareddb;
        public static IEnumerable<Warehouse> GetRootWarehouses()
        {
             shareddb=new WMSDataContext();
            return shareddb.Warehouses.Where(w => w.ParentID == null).AsEnumerable();
        }

        public static void DisposeDataContext()
        {
            if (shareddb != null)
            {
                shareddb.Dispose();
            }
        }

        public static List<usp_Warehouse_GetWarehousesResult> GetChildren(Guid warehouseid, int pageIndex, int pageSize, string sortString, string warehouseCode, string warehouseName)
        {
            using (WMSDataContext db = new WMSDataContext())
            { 
                StringBuilder whereString = new StringBuilder();
                whereString.Append("a.IsDeleted=0");
                whereString.AppendFormat(" and a.ParentId='{0}'", warehouseid.ToString());
                if(!string.IsNullOrEmpty(warehouseCode))
                {
                    whereString.AppendFormat(" and a.WarehouseCode like '%{0}%'", warehouseCode.ToLower());
                }
                if(!string.IsNullOrEmpty(warehouseName))
                {
                    whereString.AppendFormat(" and a.WarehouseName like '%{0}%'", warehouseName.ToLower());
                }

                return db.usp_Warehouse_GetWarehouses(sortString, pageSize, pageIndex, whereString.Length == 0 ? null : whereString.ToString()).ToList<usp_Warehouse_GetWarehousesResult>();
            }
        }        

        public static Warehouse GetWarehouse(Guid id)
        {
            shareddb = new WMSDataContext();

            Warehouse obj = shareddb.Warehouses.Single(p => p.WarehouseID == id);
            return obj;                 
            
        }

        public static bool CreateWarehouse(string warehouseCode, string warehouseName, char nodeType, Guid parentid, string desc,
            bool hasLocation, bool hasArea, bool hasBatchNo,Guid userid)
        {
            using (WMSDataContext db = new WMSDataContext())
            {
                int iCount=db.Warehouses.Count(p=>p.WarehouseCode.ToLower()==warehouseCode.ToLower());
                if (iCount > 0)
                {
                    return false;
                }
                try
                {
                    Warehouse ws = new Warehouse();
                    ws.WarehouseCode = warehouseCode;
                    ws.WarehouseName = warehouseName;
                    ws.NodeType = nodeType;
                    ws.HasArea = hasArea;
                    ws.HasBatchNo = hasBatchNo;
                    ws.HasLocation = hasLocation;
                    ws.ParentID = parentid;
                    ws.CreatedBy = userid;
                    ws.CreatedDate = DateTime.Now;
                    db.Warehouses.InsertOnSubmit(ws);
                    db.SubmitChanges();
                    return true;
                }
                catch(System.Data.SqlClient.SqlException)
                {
                    return false;
                }
            }
        }

        public static bool DeleteWarehouses(Guid id)
        {
            using (WMSDataContext db = new WMSDataContext())
            {
                try
                {
                    Warehouse obj = db.Warehouses.Single(p => p.WarehouseID == id);
                    obj.IsDeleted = true;
                    db.SubmitChanges();
                    return true;
                }
                catch (ChangeConflictException)
                {
                    return false;
                }
            }     
        }

        public static bool UpdateWarehouse(Guid warehouseid,string warehouseCode, string warehouseName, char nodeType, Guid parentid, string desc,
            bool hasLocation, bool hasArea, bool hasBatchNo, Guid userid)
        {
            using (WMSDataContext db = new WMSDataContext())
            {
                try
                {
                    Warehouse ws = db.Warehouses.Single(p => p.WarehouseID == warehouseid);
                    ws.WarehouseName = warehouseName;
                    ws.WarehouseCode = warehouseCode;
                    ws.NodeType = nodeType;
                    ws.ParentID = parentid;
                    ws.Description = desc;
                    ws.HasArea=hasArea;
                    ws.HasLocation=hasLocation;
                    ws.HasBatchNo = hasBatchNo;
                    ws.ModifiedBy = userid;
                    ws.ModifiedDate = DateTime.Now;
                    db.SubmitChanges();
                    return true;
                }
                catch (ChangeConflictException)
                {
                    return false;
                }
            }      
        }
    }
}
